package com.bum.game.util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;

public class RequestUtil {
	public static void redirect(HttpServletResponse response, String url) {
		response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
		response.setHeader("Location", url);
	}

	public static String getPath(HttpServletRequest request) {
		String path = request.getServletPath()
				+ StringUtils.trimToEmpty(request.getPathInfo());
		if (path != null && path.startsWith("/")) {
			return path.substring(1);
		}
		return path;
	}

	public static String getSessionString(HttpServletRequest request, String attributeName) {
		HttpSession session = request.getSession();
		if (session.getAttribute(attributeName) == null)
			return null;
		else
			return session.getAttribute(attributeName).toString();
	}
	
	public static void removeAttribute(HttpServletRequest request, String attributeName) {
		HttpSession session = request.getSession(false);
		if (session != null)
			session.removeAttribute(attributeName);
	}

	public static String getSessionStringAndRemove(HttpServletRequest request, String attributeName) {
		String value = getSessionString(request, attributeName);
		removeAttribute(request, attributeName);
		return value;
	}

}
